home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / clients / fhostcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-02  |  12.0 KB  |  473 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1993 by Michael Meskes                               *
  3.     *  (meskes@ulysses.informatik.rwth-aachen.de)                         *
  4.     *                                                                     *
  5.     *  You may copy or modify this file in any manner you wish, provided  *
  6.     *  that this notice is always included, and that you hold the author  *
  7.     *  harmless for any loss or damage resulting from the installation or *
  8.     *  use of this software.                                              *
  9.     \*********************************************************************/
  10.  
  11. #include "tweak.h"
  12. #include "client_def.h"
  13. #include "c_extern.h"
  14. #include <stdio.h>
  15. #include "my-string.h"
  16. #ifndef VMS
  17. #include <pwd.h>
  18. #else
  19. #include "pwd.h"
  20. #endif
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include <ctype.h>
  25.  
  26. #ifdef HOST_LOOKUP
  27. #include <netdb.h>
  28. #endif
  29.  
  30. #ifdef VMS
  31. #define FSP_STAT vms_stat
  32. #else
  33. #define FSP_STAT stat
  34. #endif
  35.  
  36. #define NUMBER 1
  37. #define NAME 2
  38.  
  39. int delay = -1, local_port = -1, timeout = -1, trace = -1, port = -1;
  40. char *local_dir = NULL, *filename = NULL, *password = NULL;
  41. char *hostname = NULL, *hostaddr = NULL, *dir = NULL;
  42.  
  43. extern char *optarg;
  44. extern int optind;
  45.  
  46. char *buffer=0L,*home="/";
  47.  
  48. void host_usage PROTO0((void)) /* print usage message */
  49. {
  50.   fprintf(stderr,"Usage: fhost [-d delay] [-p local port] [-l local dir]\n");
  51.   fprintf(stderr,"             [-o timeout] [-t trace] [-w password]\n");
  52.   fprintf(stderr,"             [-f filename] [-h [number|name]]\n");
  53.   fprintf(stderr,"             [host port [directory] | abbreviation]\n");
  54.   exit(0);
  55. }
  56.  
  57. char *load_file PROTO1(char *, filename) /* load resource file */
  58. {
  59.   FILE *stream;
  60.   long i,length;
  61.   struct stat sf;
  62.   char *f1=".fsp_prof", *f2=0L, *f3=FSPRC;
  63.   
  64.   if (!filename) {
  65.     if (!FSP_STAT(f1,&sf)) filename=f1;
  66.     else {
  67.       f2=(char *)malloc (strlen(home) + 7);
  68.       if (!f2) {
  69.     perror("malloc");
  70.     return(0L);
  71.       }
  72.       sprintf (f2,"%s/.fsp_prof",home);
  73.       if (!FSP_STAT(f2,&sf)) filename=f2;
  74.       else filename=f3;
  75.     }
  76.   }
  77.   stream=fopen(filename,"r");
  78.   if (!stream) {
  79.     perror(filename);
  80.     if (f2) free(f2); return(0L);
  81.   }
  82.   i=fseek(stream,0L,2);
  83.   if (i) {
  84.     perror("fseek");
  85.     fclose(stream);
  86.     if (f2) free(f2);
  87.     return(0L);
  88.   }
  89.   length=ftell(stream);
  90.   if (!length) {
  91.     perror("ftell");
  92.     fclose(stream);
  93.     if (f2) free(f2);
  94.     return(0L);
  95.   }
  96.   buffer=(char *)calloc(length,sizeof(char));
  97.   if (!buffer) {
  98.     perror("malloc");
  99.     fclose(stream);
  100.     if (f2) free(f2);
  101.     return(0L);
  102.   }
  103.   i=fseek(stream,0L,0);
  104.   if (i) {
  105.     perror("fseek");
  106.     free(buffer);
  107.     fclose(stream);
  108.     if (f2) free(f2);
  109.     return(0L);
  110.   }
  111.   fread(buffer,(long)sizeof(char),length,stream);
  112.   if (ferror(stream)) {
  113.     perror("fread");
  114.     free(buffer);
  115.     fclose(stream);
  116.     if (f2) free(f2);
  117.     return(0L);
  118.   }
  119.   i=fclose(stream);
  120.   if (i) {
  121.     perror("fclose");
  122.     free(buffer);
  123.     if (f2) free(f2);
  124.     return(0L);
  125.   }
  126.   if (f2) free(f2);
  127.   return(buffer);
  128. }
  129.  
  130. /* get data out of resource file */
  131. void parse_file PROTO2(char *, filename, char *, abb)
  132. {
  133.   char *ptrh,*ptrp,*ptra,*ptre;
  134.   int found=0;
  135.   
  136.   ptrh=buffer=load_file(filename);
  137.   if (!buffer) exit(1);
  138.   while ((ptrh=(char *)strstr(ptrh,"host ")) && !found) {
  139.     ptrh+=5;
  140.     for (ptre=ptrh;*ptre!='\n';ptre++);
  141.     ptra=(char *)strstr(ptrh,abb);
  142.     if (ptra && ptra<ptre) {
  143.       found=1;
  144.       /* get host name or number */
  145.       for (ptrp=ptrh;!hostname && *ptrp!=' ' && *ptrp!='\n';ptrp++)
  146.     if (!isdigit(*ptrp) && *ptrp!='.') hostname=ptrh;
  147.       if (!hostname) hostaddr=ptrh;
  148.       /* does the othe one exist, too? */
  149.       for (;*ptrp!=' ' && *ptrp!='\n';ptrp++);
  150.       for (;*ptrp==' ';ptrp++); 
  151.       if (*ptrp!='\n') {
  152.     if (hostaddr) hostname=ptrp;
  153.     else if (hostname) {
  154.       hostaddr=ptrp;
  155.       for (;*ptrp!=' ' && *ptrp!='\n';ptrp++)
  156.         if (!isdigit(*ptrp) && *ptrp!='.') {
  157.           fprintf(stderr, "fhost: syntax error in setup file\n");
  158.           free(buffer);
  159.           exit(1);
  160.         }
  161.     }
  162.       }
  163.       for (ptrh++;*ptrh!='\n';ptrh++);
  164.       ptrp=(char *)strstr(ptrh,"fsp port ");
  165.       if (!ptrp) {
  166.     fprintf(stderr,"fhost: syntax error in setup file\n");
  167.     free(buffer);
  168.     exit(1);
  169.       }
  170.       port=atol(ptrp+9);
  171.       ptre=(char *)strstr(ptrh,"host "); /* begin next host */
  172.       /* search for environment values */
  173.       if (delay==-1) {
  174.     ptra=(char *)strstr(ptrh,"delay ");
  175.     if (ptra && (!ptre || ptra<ptre)) delay=atol(ptra+6);
  176.       }
  177.       if (timeout==-1) {
  178.     ptra=(char *)strstr(ptrh,"timeout ");
  179.     if (ptra && (!ptre || ptra<ptre)) timeout=atol(ptra+8);
  180.       }
  181.       if (trace==-1) {
  182.     ptra=(char *)strstr(ptrh,"trace ");
  183.     if (ptra && (!ptre || ptra<ptre))    {
  184.       for (ptra+6;*ptra!='o' && *ptra!='\n';ptra++);
  185.       if (*ptra=='o') trace=(ptra[1]=='n') ? 1 : 0;
  186.       else {
  187.         fprintf(stderr, "fhost: syntax error in setup file\n");
  188.         free(buffer);
  189.         exit(1);
  190.       }
  191.     }
  192.       }
  193.       if (local_port==-1) {
  194.     ptra=(char *)strstr(ptrh,"local port ");
  195.     if (ptra && (!ptre || ptra<ptre)) local_port=atol(ptra+11);
  196.       }
  197.       if (local_dir==0L) {
  198.     ptra=(char *)strstr(ptrh,"local directory ");
  199.     if (ptra && (!ptre || ptra<ptre)) local_dir=ptra+16;
  200.       }
  201.       if (dir==0L) {
  202.     ptra=(char *)strstr(ptrh,"fsp directory ");
  203.     if (ptra && (!ptre || ptra<ptre)) dir=ptra+14;
  204.       }
  205.       if (password==0L) {
  206.     ptra=(char *)strstr(ptrh,"password ");
  207.     if (ptra && (!ptre || ptra<ptre)) password=ptra+16;
  208.       }
  209.     }
  210.   }
  211.   if (!found) fprintf(stderr,"host %s not found\n",abb);
  212.   return;
  213. }
  214.  
  215. void list_file PROTO1(char *, filename) /* list resource file */
  216. {
  217.   char *ptrh,*ptrp,*ptre;
  218.   
  219.   ptrh=buffer=load_file(filename);
  220.   if (!buffer) exit(1);
  221.   while (ptrh=(char *)strstr(ptrh,"host ")) {
  222.     ptrh+=5;
  223.     fprintf(stderr,"host: ");
  224.     for (;*ptrh!='\n';ptrh++) fprintf(stderr,"%c",*ptrh);
  225.     ptrp=(char *)strstr(ptrh,"fsp port ");
  226.     ptre=(char *)strstr(ptrh,"host ");
  227.     if (!ptrp || (ptre && ptrp>=ptre)) {
  228.       fprintf(stderr,"fhost: syntax error in setup file\n");
  229.       free(buffer);
  230.       exit(1);
  231.     }
  232.     fprintf(stderr," port: ");
  233.     for (ptrp+=9;*ptrp!='\n';ptrp++) fprintf(stderr,"%c",*ptrp);
  234.     fprintf(stderr,"\n\n");
  235.   }
  236.   return;
  237. }
  238.  
  239. int main PROTO3(int, argc, char **, argv, char **, envp)
  240. {
  241.   int optletter,csh,host=0;
  242.   register char *p;
  243.   char *log,*homedir="/";
  244.   struct passwd *pw=0L;
  245.   struct hostent *hp;
  246.   long addr;
  247.  
  248. #ifndef VMS  
  249.   log = (char *)getlogin();
  250.   if (log) pw = getpwnam(log);
  251.   if (!pw) pw = getpwuid(getuid());
  252.   if (pw) {
  253.     /*
  254.      * Figure out what shell we're using.  A hack, we look for a shell
  255.      * ending in "csh".
  256.      */
  257. /*    csh = !strcmp(pw->pw_shell + strlen(pw->pw_shell) - 3, "csh"); */
  258.     csh = 1;        /* KK PATCH */
  259.     home = pw->pw_dir;   /* for default search for file .fsp_prof*/
  260.   }
  261. #endif
  262.  
  263.   while ((optletter=getopt(argc, argv,"d:p:l:t:o:f:h:w:")) != EOF)
  264.     switch (optletter) {
  265.       case 'd':
  266.         delay=atol(optarg); /* FSP_DELAY */
  267.     break;
  268.       case 'p':
  269.     local_port=atol(optarg); /* FSP_LOCAL_PORT */
  270.     break;
  271.       case 'w':
  272.     password=optarg; /* FSP_PASSWORD */
  273.     break;
  274.       case 'l':
  275.     local_dir=optarg; /* FSP_LOCAL_DIR */
  276.     break;
  277.       case 'o':
  278.     timeout=atol(optarg); /* FSP_TIMEOUT */
  279.     break;
  280.       case 't':
  281.     if (!strcmp(optarg,"on")) trace=1;  /* FSP_TRACE */
  282.     else if (!strcmp(optarg,"off")) trace=0;
  283.     else host_usage();
  284.     break;
  285.       case 'f':
  286.     filename=optarg; /* file name */
  287.     break;
  288.       case 'h':
  289.     if (!strcmp(optarg,"number")) host=NUMBER; /* host mode */
  290.     else if (!strcmp(optarg,"name")) host=NAME;
  291.     else host_usage();
  292.     break;
  293.       default:
  294.     host_usage();
  295.     break;
  296.     }
  297.  
  298.   if(argc > optind+1 && !filename) { /* host and port and no filename given */
  299.     for (p=argv[optind];!hostname && *p && *p!='\n';p++)
  300.       if (!isdigit(*p) && *p!='.') hostname=argv[optind];
  301.     if (!hostname) hostaddr=argv[optind];
  302.     port=atol(argv[optind+1]);
  303.     if (argc > optind + 1) dir=argv[optind+2]; /* directory given, too */
  304.   } else if (argc > optind) { /* abbreviation given */
  305.     parse_file(filename, argv[optind]);
  306.   } else { /* list or set command-line options */
  307.     if (filename || argc==1) {  /* list only */
  308.       list_file(filename);
  309.       exit(0);
  310.     }
  311.   }
  312.   if (delay>=0) {
  313. #ifdef VMS
  314.     printf("$define/nolog FSP_DELAY %d\n",delay);
  315. #else
  316.     if (csh) printf("setenv FSP_DELAY %d;\n",delay);
  317.     else printf("FSP_DELAY=%d;\nexport FSP_DELAY;\n",delay);
  318. #endif
  319.   }
  320.   if (local_port>=0) {
  321. #ifdef VMS
  322.     printf("$define/nolog FSP_LOCALPORT %d\n",local_port);
  323. #else
  324.     if (csh) printf("setenv FSP_LOCALPORT %d;\n",local_port);
  325.     else printf("FSP_LOCALPORT=%d;\nexport FSP_LOCALPORT;\n",local_port);
  326. #endif
  327.   }
  328.   if (trace>=0) {
  329.     if (csh) {
  330. #ifdef VMS
  331.       printf("$define/nolog FSP_TRACE \"1\"\n");
  332. #else
  333.       if (trace) printf("setenv FSP_TRACE;\n");
  334.       else printf("unsetenv FSP_TRACE;\n");
  335. #endif
  336.     } else {
  337. #ifdef VMS
  338.       printf("$deassign FSP_TRACE\n");
  339. #else
  340.       if (trace) printf("FSP_TRACE;\nexport FSP_TRACE;\n");
  341.       else printf("unset FSP_TRACE;\n");
  342. #endif
  343.     }
  344.   }
  345.   if (timeout>=0) {
  346. #ifdef VMS
  347.     printf("$define/nolog FSP_TIMEOUT %d\n",timeout);
  348. #else
  349.     if (csh) printf("setenv FSP_TIMEOUT %d;\n",timeout);
  350.     else printf("FSP_TIMEOUT=%d;\nexport FSP_TIMEOUT;\n",timeout);
  351. #endif
  352.   }
  353.   if (port>=0) {
  354. #ifdef VMS
  355.     printf("$define/nolog FSP_PORT %d\n",port);
  356. #else
  357.     if (csh) printf("setenv FSP_PORT %d;\n",port);
  358.     else printf("FSP_PORT=%d;\nexport FSP_PORT;\n",port);
  359. #endif
  360.   }
  361.   if (local_dir) {
  362. #ifdef VMS
  363.     printf("$define/nolog FSP_LOCAL_DIR \"");
  364. #else
  365.     if (csh) printf("setenv FSP_LOCAL_DIR ");
  366.     else printf("FSP_LOCAL_DIR=");
  367. #endif
  368.     for (p=local_dir;*p && *p!='\n' && *p!=' ';p++) printf("%c",*p);
  369. #ifdef VMS
  370.     printf("\"\n");
  371. #else
  372.     if (csh) printf(";\n");
  373.     else printf(";\nexport FSP_LOCAL_DIR;\n");
  374. #endif
  375.   }
  376.   if (password) {
  377. #ifdef VMS
  378.     printf("$define/nolog FSP_PASSWORD \"");
  379. #else
  380.     if (csh) printf("setenv FSP_PASSWORD ");
  381.     else printf("FSP_PASSWORD=");
  382. #endif
  383.     for (p=password;*p && *p!='\n' && *p!=' ';p++) printf("%c",*p);
  384. #ifdef VMS
  385.     printf("\"\n");
  386. #else
  387.     if (csh) printf(";\n");
  388.     else printf(";\nexport FSP_PASSWORD;\n");
  389. #endif
  390.   }
  391.   if (hostname || hostaddr) {
  392. #ifdef VMS
  393.     printf("$define/nolog FSP_HOST \"");
  394. #else
  395.     if (csh) printf("setenv FSP_HOST ");
  396.     else printf("FSP_HOST=");
  397. #endif
  398.     if(hostname) {
  399.       for(p = hostname; *p && *p!='\n' && *p!= ' '; p++);
  400.       *p = 0;
  401.     }
  402.     if(hostaddr) {
  403.       for(p=hostaddr;*p && *p !='\n' && *p!=' ';p++);
  404.       *p = 0;
  405.     }
  406.     if(host==NAME && !hostname) {
  407. #if HOST_LOOKUP
  408.       addr=inet_addr(hostaddr);
  409.       if (hp=gethostbyaddr((char *) &addr, sizeof(addr), AF_INET))
  410.     hostname= (char *)hp->h_name;
  411. #endif
  412.       if (!hostname) host=NUMBER;
  413.     }
  414.     if (host==NUMBER && !hostaddr) { /* look for number */
  415. #if HOST_LOOKUP
  416.       if (hp=gethostbyname(hostname))
  417.     hostaddr=(char *)inet_ntoa(*(struct in_addr *) * hp->h_addr_list);
  418. #endif
  419.       if (!hostaddr) host=NAME;
  420.     }
  421.     if (!host) {
  422.       if (hostaddr) host=NUMBER;
  423.       else if (hostname) host=NAME;
  424.       else {
  425.     fprintf(stderr,"fhost: No host given!");
  426.     if (buffer) free(buffer);
  427.     exit(1);
  428.       }
  429.     }
  430.     printf("%s", (host==NAME)? hostname : hostaddr);
  431. #ifdef VMS
  432.     printf("\"\n");
  433. #else
  434.     if (csh) printf(";\n");
  435.     else printf(";\nexport FSP_HOST;\n");
  436. #endif
  437.     if (!dir) dir=homedir; /* if host is set we need this */
  438.   }
  439.   if (dir) {
  440. #ifdef VMS
  441.     printf("$define/nolog FSP_DIR \"");
  442. #else
  443.     if (csh) printf("setenv FSP_DIR ");
  444.     else printf("FSP_DIR=");
  445. #endif
  446.     for (p=dir;*p && *p!='\n' && *p!=' ';p++) printf("%c",*p);
  447. #ifdef VMS
  448.     printf("\"\n");
  449. #else
  450.     if (csh) printf(";\n");
  451.     else printf(";\nexport FSP_DIR;\n");
  452. #endif
  453.   }
  454.  
  455. #ifdef VMS
  456.   if (hostname) printf("$define/nolog FSP_NAME \"");
  457.   else printf("$deassign FSP_NAME\n");
  458. #else
  459.   if (csh) printf("setenv FSP_NAME \"");
  460.   else printf("FSP_NAME=\"");
  461. #endif
  462.   if (hostname)
  463.     for (p=hostname;*p && *p!='\n' && *p!=' ';p++) printf("%c",*p);
  464. #ifdef VMS
  465.   if (hostname) printf("\"\n");
  466. #else
  467.   if (csh) printf("\";\n");
  468.   else printf("\";\nexport FSP_NAME;\n");
  469. #endif
  470.   if (buffer) free(buffer);
  471.   exit(0);
  472. }
  473.